package dblog

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm/logger"
	"time"
)

type Glog struct {
	LogLevel logger.LogLevel
}

func New() *Glog {
	return new(Glog)
}

func (l *Glog) LogMode(level logger.LogLevel) logger.Interface {
	l.LogLevel = level
	return l
}

func (l *Glog) Info(ctx context.Context, msg string, data ...interface{}) {

	logx.WithContext(ctx).Debugf(msg, data)
}
func (l *Glog) Warn(ctx context.Context, msg string, data ...interface{}) {

	logx.WithContext(ctx).Infof(msg, data)
}

func (l *Glog) Error(ctx context.Context, msg string, data ...interface{}) {

	logx.WithContext(ctx).Errorf(msg, data)
}

func (l *Glog) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
	//这块的逻辑可以自己根据业务情况修改
	elapsed := time.Since(begin)
	sql, rows := fc()
	logx.WithContext(ctx).WithDuration(elapsed).WithFields(logx.LogField{Key: "sql", Value: "【gorm】"}).
		Slowf("Trace sql: %v  row： %v  err: %v", sql, rows, err)
}
